﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BitPortal.Models;
using BitPortal.Business;
using Ext.Net;
namespace BitPortal.Admin.Product.Category.Module
{
    public partial class ProductCategoryTree : System.Web.UI.UserControl
    {
        public Ext.Net.TreePanel TreepanelCategory
        {
            get { return this.CategoryTree; }
        }

        public void CheckNode(string[] nodeIDs)
        {
            foreach (string id in nodeIDs)
            {
                if (!string.IsNullOrEmpty(id))
                {
                    var node = FindNodeById(id);
                    if (node != null)
                    {
                        node.Checked = ThreeStateBool.True;
                    }
                }
            }
        }

        public Ext.Net.TreeNode FindNodeById(string nodeID)
        {
            return FindNodeById(CategoryTree.Root, nodeID);
        }

        public Ext.Net.TreeNode FindNodeById(Ext.Net.TreeNodeCollection items, string nodeID)
        {
            foreach (var item in items)
            {
                if (item.NodeID == nodeID)
                    return item as Ext.Net.TreeNode;
                else
                {
                    var node = FindNodeById((item as Ext.Net.TreeNode).Nodes, nodeID);
                    if (node != null)
                        return node;
                }
            }
            return null;

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                BindTreeNode();
            }
        }

        public void BindTreeNode(string[] checkedIDs = null)
        {
            Ext.Net.TreeNode node = CreateNode(null,checkedIDs);
            CategoryTree.Root.Clear();
            CategoryTree.Root.Add(node);
            CategoryTree.RootVisible = true;
            
        }

        public Ext.Net.TreeNode CreateNode(ProductCategory category,string[] checkedIDs = null)
        {
            //add curent category
            Ext.Net.TreeNode node = new Ext.Net.TreeNode("Nhóm gốc");
            node.NodeID = "0";
            if (category != null)
            {
                node.NodeID = category.CategoryID.ToString();
                node.Text = category.Name;
                node.Qtip = category.Description;
                node.Checked = (checkedIDs != null && checkedIDs.Contains(node.NodeID.ToString()))? ThreeStateBool.True : ThreeStateBool.False;
            }
            //add childs categories 
            foreach (ProductCategory ac in category.GetChilds())
            {
                node.Nodes.Add(CreateNode(ac, checkedIDs));
            }
            //return node
            return node;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            //
            //CheckNode(new string[] { "1", "2" });
        }

    }
}